<template>
  <el-dialog
    :title="!dataForm.uid ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户名" prop="uname">
      <el-input v-model="dataForm.uname" placeholder="用户名"></el-input>
    </el-form-item>
    <el-form-item label="手机号" prop="mobile">
      <el-input v-model="dataForm.mobile" placeholder="手机号"></el-input>
    </el-form-item>
    <el-form-item label="昵称" prop="nickname">
      <el-input v-model="dataForm.nickname" placeholder="昵称"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="password">
      <el-input v-model="dataForm.password" placeholder="密码"></el-input>
    </el-form-item>
    <el-form-item label="电子邮箱" prop="email">
      <el-input v-model="dataForm.email" placeholder="电子邮箱"></el-input>
    </el-form-item>
    <el-form-item label="登录时间" prop="logintime">
      <el-input v-model="dataForm.logintime" placeholder="登录时间"></el-input>
    </el-form-item>
    <el-form-item label="性别" prop="gender">
      <el-input v-model="dataForm.gender" placeholder="性别"></el-input>
    </el-form-item>
    <el-form-item label="角色  0-普通用户 1-商户 2-管理员" prop="role">
      <el-input v-model="dataForm.role" placeholder="角色  0-普通用户 1-商户 2-管理员"></el-input>
    </el-form-item>
    <el-form-item label="是否是优质客户  0-不是  1-是" prop="highQuality">
      <el-input v-model="dataForm.highQuality" placeholder="是否是优质客户  0-不是  1-是"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          uid: 0,
          uname: '',
          mobile: '',
          nickname: '',
          password: '',
          email: '',
          logintime: '',
          gender: '',
          role: '',
          highQuality: ''
        },
        dataRule: {
          uname: [
            { required: true, message: '用户名不能为空', trigger: 'blur' }
          ],
          mobile: [
            { required: true, message: '手机号不能为空', trigger: 'blur' }
          ],
          nickname: [
            { required: true, message: '昵称不能为空', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '密码不能为空', trigger: 'blur' }
          ],
          email: [
            { required: true, message: '电子邮箱不能为空', trigger: 'blur' }
          ],
          logintime: [
            { required: true, message: '登录时间不能为空', trigger: 'blur' }
          ],
          gender: [
            { required: true, message: '性别不能为空', trigger: 'blur' }
          ],
          role: [
            { required: true, message: '角色  0-普通用户 1-商户 2-管理员不能为空', trigger: 'blur' }
          ],
          highQuality: [
            { required: true, message: '是否是优质客户  0-不是  1-是不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.uid = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.uid) {
            this.$http({
              url: this.$http.adornUrl(`/user/user/info/${this.dataForm.uid}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.uname = data.user.uname
                this.dataForm.mobile = data.user.mobile
                this.dataForm.nickname = data.user.nickname
                this.dataForm.password = data.user.password
                this.dataForm.email = data.user.email
                this.dataForm.logintime = data.user.logintime
                this.dataForm.gender = data.user.gender
                this.dataForm.role = data.user.role
                this.dataForm.highQuality = data.user.highQuality
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/user/user/${!this.dataForm.uid ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'uid': this.dataForm.uid || undefined,
                'uname': this.dataForm.uname,
                'mobile': this.dataForm.mobile,
                'nickname': this.dataForm.nickname,
                'password': this.dataForm.password,
                'email': this.dataForm.email,
                'logintime': this.dataForm.logintime,
                'gender': this.dataForm.gender,
                'role': this.dataForm.role,
                'highQuality': this.dataForm.highQuality
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
